/*
	如有后来人，看不懂代码，请联系之前开发人员18级
	不是我们技术不到位，实在是天不时，地不利，资金不到位。
	甲方要求多，一天一个样。
	
*/
url = goloble_url
url_hidden = url_hidden
var app = new Vue({
	el: '#app',
	data: {
		//图片弹窗标识
		popup: 0,
		//url: 'http://114.215.83.227:809/admin/',
		//url_hidden: 'http://114.215.83.227:809/hiddenTroubleImg/',
		hiddenTroubleIdStatus: [{
				value: '已认证',
				label: '已认证'
			},
			{
				value: '未认证',
				label: '未认证'
			},
		],
		//获取前端选择的状态
		selectStatus: [],
		statusUserId: '',
		UserCount:0,
		// ^^^^^^^^^^
		randomPhotoList: [],
		hiddenTroubleId: 0,
		modal2: false,
		modal_loading: false,
		//状态id
		auditStateHiddenTroubleId: 0,
		//临时存储获得的图片路径
		img_path: '',
		forbidden_page:false





	},
	methods: {
		//时间戳转化
		timestampToTime(cjsj) {
			var date = new Date(date);
			var date = new Date(cjsj) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
			var Y = date.getFullYear() + '-'
			var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
			var D = date.getDate() + ' '
			var h = date.getHours() + ':'
			var m = date.getMinutes() + ':'
			var s = date.getSeconds()
			return Y + M + D + h + m + s
			console.log(timestampToTime(1533293827000))
		},

		//初始化页面 隐患列表 每次20条 
		getHideList() {
			var vm = this;
			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			//vm.pageNum = page
			//return page;
			this.$http.post(url + 'AdminGetHiddenTroubleListByPage', {
				'currentPageNo': 1,
				//'auditState':2
			}, {
				emulateJSON: true,
				headers: {
					'access_header': access_header
				}


			}).then(function(res) {
				if (res.data.status == 1) {

					vm.$Message.success('刷新成功')
					//console.log('获取到的是hide:' + res)


					vm.randomPhotoList = res.data.message
					for (var i = 0; i < vm.randomPhotoList.length; i++) {
						// 判断认证状态
						switch (vm.randomPhotoList[i].auditState) {
							case 0:
								vm.randomPhotoList[i].auditState = "未认证"
								break;
							case 1:
								vm.randomPhotoList[i].auditState = "已认证"
								break;
							default:
								break;
						}

						//判断校区campus
						switch (vm.randomPhotoList[i].campus) {
							case 1:
								vm.randomPhotoList[i].campus = "未知"
							case 2:
								vm.randomPhotoList[i].campus = "老校区"
							case 3:
								vm.randomPhotoList[i].campus = "新校区"
								break;
							default:
								break;
						}

						// 判断logicDelete 逻辑删除
						switch (vm.randomPhotoList[i].logicDelete) {
							case 1:
								vm.randomPhotoList[i].logicDelete = "未删除"
							case 2:
								vm.randomPhotoList[i].logicDelete = "已删除"
								break;
							default:
								break;
						}

						// 判断userRole
						switch (vm.randomPhotoList[i].userRole) {
							case 1:
								vm.randomPhotoList[i].userRole = "其他"

							case 2:
								vm.randomPhotoList[i].userRole = "老师"

							case 3:
								vm.randomPhotoList[i].userRole = "学生"
								break;
							default:
								break;
						}
						//时间戳转化
						vm.randomPhotoList[i].addTime = vm.timestampToTime(vm.randomPhotoList[i]
							.addTime)

						//vm.timestampToTime (vm.randomPhotoList[i].addtime)
						//console.log('addtime :' + vm.timestampToTime (vm.randomPhotoList[i].addtime))

						//console.log('addtime:' + vm.randomPhotoList[i].addTime)


					}
				} else if (res.data.status == 0) {

					vm.$Message.error('登录失效，3秒后强制登录....')


					setTimeout(() => {

						window.top.location = "login.html"
					}, 3000)

				} else {
					vm.$Message.error('不可预知错误')
				}

			}, function(res) {
				vm.$Message.error('不可预知错误,联系管理员')
			});


		},





		//点击 根据页码获取随手拍信息 20条
		accordPageHideList(page) {
			var vm = this;
			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			//vm.pageNum = page
			//return page;
			this.$http.post(url + 'AdminGetHiddenTroubleListByPage', {
				'currentPageNo': page,
				//'auditState':2
			}, {
				emulateJSON: true,
				headers: {
					'access_header': access_header
				}


			}).then(function(res) {
				if (res.data.status == 1) {
					if(res.data.message.length == 0)
					{
						vm.$Message.error('没有更多数据，请点击右上角刷新回到第一页')
						vm.forbidden_page = true;
					}else{
						vm.$Message.success('刷新成功')
					}
					
					

					
					//console.log('获取到的是hide:' + res.data.message)


					vm.randomPhotoList = res.data.message
					// golblename(vm.randomPhotoList)

					// console.log('获取到的是9999:' + golblename())

					for (var i = 0; i < vm.randomPhotoList.length; i++) {
						// 判断认证状态
						switch (vm.randomPhotoList[i].auditState) {
							case 0:
								vm.randomPhotoList[i].auditState = "未认证"
								break;
							case 1:
								vm.randomPhotoList[i].auditState = "已认证"
								break;
							default:
								break;
						}

						//判断校区campus
						switch (vm.randomPhotoList[i].campus) {
							case 1:
								vm.randomPhotoList[i].campus = "未知"
							case 2:
								vm.randomPhotoList[i].campus = "老校区"
							case 3:
								vm.randomPhotoList[i].campus = "新校区"
								break;
							default:
								break;
						}

						// 判断logicDelete 逻辑删除
						switch (vm.randomPhotoList[i].logicDelete) {
							case 1:
								vm.randomPhotoList[i].logicDelete = "未删除"
							case 2:
								vm.randomPhotoList[i].logicDelete = "已删除"
								break;
							default:
								break;
						}

						// 判断userRole
						switch (vm.randomPhotoList[i].userRole) {
							case 1:
								vm.randomPhotoList[i].userRole = "其他"

							case 2:
								vm.randomPhotoList[i].userRole = "老师"

							case 3:
								vm.randomPhotoList[i].userRole = "学生"
								break;
							default:
								break;
						}
						//时间戳转化
						vm.randomPhotoList[i].addtime = vm.timestampToTime(vm.randomPhotoList[i]
							.addtime)
						//vm.timestampToTime (vm.randomPhotoList[i].addtime)
						console.log('addtime :' + vm.timestampToTime(vm.randomPhotoList[i].addtime))

						console.log('userRole:' + vm.randomPhotoList[i].userRole)


					}
				} else if (res.data.status == 0) {

					vm.$Message.error('获取失败，请重新登录')

				} else {
					vm.$Message.error('不可预知错误')
				}

			}, function(res) {
				vm.$Message.error('不可预知错误,联系管理员')
			});


		},

		//获取hiddenTroubleId用于修改状态
		getHiddenTroubleId(id) {
			this.auditStateHiddenTroubleId = id
			console.log("auditStateHiddenTroubleId：" + this.auditStateHiddenTroubleId)
		},
		
		
		//管理员通过hiddenTroubleId、要修改成的状态，修改隐患信息审核状态
		// 必填：hiddenTroubleId、auditState
		// 选填：
		// http://192.168.3.2:809/admin/AdminChangeHiddenTroubleAuditState





		// 修改隐患认证状态
		changeStatus(index) {
			var vm = this
			// console.log("changeStatusindex："+index)
			// console.log("111111statusUserId："+vm.statusUserId)
			//    console.log("changeStatusindex："+vm.selectStatus[index])

			if (vm.selectStatus[index] == "未认证") {

				var auditState = 0

			} else if (vm.selectStatus[index] == "已认证") {

				var auditState = 1
				console.log("HiddenTroubleId：" + auditState)
			}

			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			this.$http.post(url + 'AdminChangeHiddenTroubleAuditState', {
				'hiddenTroubleId': vm.auditStateHiddenTroubleId,
				'auditState': auditState
			}, {
				emulateJSON: true,
				headers: {
					'access_header': access_header
				}
			}).then(function(res) {
				if (res.data.status == 1) {
					//vm.getUserList()
					//console.log('111获取到的是:' + res.data.prompt)
					

					this.$Message.success('修改用户认证状态成功');
					



				} else if (res.data.status == 0) {

					vm.$Message.error('失败')
					location.reload(); //刷新页面

					//console.log('111获取到的是:' + res.data.message)

				} else {
					vm.$Message.error('不可预知错误')
				}

			}, function(res) {
				vm.$Message.error('不可预知错误,联系管理员')
			});






		},



		//获取用户hiddenTroubleId  删除
		hideDel(hiddenTroubleId) {
			console.log('enter:' + hiddenTroubleId)
			this.hiddenTroubleId = hiddenTroubleId;
		},

		//删除 所有图片 所有图片 根据hiddenTroubleId
		del() {
			this.modal_loading = true;
			setTimeout(() => {
				this.modal_loading = false;
				this.modal2 = false;
				//this.$Message.success('删除成功');
			}, 1000);
			// console.log('获取到的是:' + id)

			var vm = this;
			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			this.$http.post(url + 'AdminDelHiddenTroubleById', {
				'hiddenTroubleId': vm.hiddenTroubleId,
			}, {
				emulateJSON: true,
				headers: {
					'access_header': access_header
				}
			}).then(function(res) {
				if (res.data.status == 1) {
					vm.getHideList()
					//console.log('hide获取到的是:' + vm.userList[i].userRole)


				} else if (res.data.status == 0) {

					vm.$Message.error('删除失败')

				} else {
					vm.$Message.error('不可预知错误')
				}

			}, function(res) {
				vm.$Message.error('不可预知错误,联系管理员')
			});

		},

		//所有人通过hiddenTroubleId获取隐患信息图片列表
		// 必填：hiddenTroubleId
		// 选填：
		// http://192.168.3.2:809/hiddenTroubleImg/getHiddenTroubleImgList

		get_hiddenimg_url(hiddenTroubleId) {
			// this.$Message.error('开始')
			console.log('hiddenTroubleId:' + hiddenTroubleId)
			var vm = this;
			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			this.$http.post(url_hidden + 'getHiddenTroubleImgList', {
				'hiddenTroubleId': hiddenTroubleId,
			}, {
				emulateJSON: true,
				headers: {
					'access_header': access_header
				}
			}).then(function(res) {
				if (res.data.status == 1) {

					console.log('more获取到的是:' + res.data)
					console.log('more获取到的是:' + res.data.message[0].hiddenTroubleImgUrl)
					vm.getimg(res.data.message[0].hiddenTroubleImgUrl)

				} else if (res.data.status == 0) {

					vm.$Message.error('删除失败')

				} else {
					vm.$Message.error('不可预知错误')
				}

			}, function(res) {
				vm.$Message.error('不可预知错误,联系管理员')
			});
		},
		//获取图片 返回一个blob类型的字符串
		getimg(url) {
			var vm = this;
			// url =
			// 	'http://114.215.83.227:809/userImg/2018250027/workImg/2bfac844-df59-4496-a7af-e2ab3b9f120c_2000 (3).jpg'
			var blob_url = '';
			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			axios({
				method: 'get',
				url: url, // 请求地址
				headers: {
					'access_header': access_header
				},
				responseType: 'blob' // 设置接收格式为blob格式
			}).then(res => {
				// console.log(res)
				if (res && res.data && res.data.size) {
					const dataInfo = res.data
					let reader = new window.FileReader()
					// 使用readAsArrayBuffer读取文件, result属性中将包含一个 ArrayBuffer 对象以表示所读取文件的数据
					reader.readAsArrayBuffer(dataInfo)
					reader.onload = function(e) {
						const result = e.target.result
						const contentType = dataInfo.type
						// 生成blob图片,需要参数(字节数组, 文件类型)
						const blob = new Blob([result], {
							type: contentType
						})
						// 使用 Blob 创建一个指向类型化数组的URL, URL.createObjectURL是new Blob文件的方法,可以生成一个普通的url,可以直接使用,比如用在img.src上
						const url = window.URL.createObjectURL(blob)
						vm.img_path = url
						// console.log(
						// 	vm.img_path) // 产生一个类似 blob:d3958f5c-0777-0845-9dcf-2cb28783acaf 这样的URL字符串


					}
				}
			});
		},

		//打开图片弹窗  后车图
		showpopup(hiddenTroubleId) {
			var vm = this;
			vm.loading();
			vm.img_path = '';
			vm.get_hiddenimg_url(hiddenTroubleId);
			setTimeout(() => {

				this.popup = 1;
				//console.log(hiddenTroubleId)

			}, 1000);

		},
		//关闭图片弹窗
		closepopup() {
			this.popup = 0;
		},
		//加载中
		loading() {
			const msg = this.$Message.loading({
				content: 'Loading...',
				duration: 0
			});
			setTimeout(msg, 1000);
		},


	},
	mounted() {
		this.getHideList()

	},
	created() {


	},
	watch: {

	}


});
